package com.fansl.allround.upms.helper;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.fansl.allround.common.core.constant.enums.RegionLevelEnum;
import com.fansl.allround.upms.api.entity.SysRegion;
import com.fansl.allround.upms.helper.region.*;
import com.fansl.allround.upms.service.SysRegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.Assert;
import us.codecraft.webmagic.Spider;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author fansl
 * @Description: TODO
 * @date 2019/8/23 17:38
 */
@Slf4j
public class RegionHelper {
    public static String PREFIX_REGION_HELP_URL_KEY_IN_REDIS = "region_crawl:";
    public static String HELP_URL = "helpUrl";

    public static void executeSpider(RegionLevelEnum regionLevel,
                                     List<String> pageUrlList, Boolean recursionFlag,
                                     SysRegionService sysRegionService,
                                     RedisTemplate redisTemplate) {
        Assert.notEmpty(pageUrlList, "pageUrlList is empty");
        if (recursionFlag == null) {
            recursionFlag = false;
        }
        String redisKey = PREFIX_REGION_HELP_URL_KEY_IN_REDIS + regionLevel.getLevel();
        Long count = redisTemplate.opsForSet().add(redisKey, ArrayUtil.toArray(pageUrlList, String.class));
        log.info("需要爬取的地址数量：{}，存入redis的数量：{}", pageUrlList.size(), count);

        switch (regionLevel) {
            case PROVINCE: {
                Spider.create(new ProvinceProcessor(sysRegionService, redisTemplate, recursionFlag))
                        //从"https://github.com/code4craft"开始抓
                        .addUrl(ArrayUtil.toArray(pageUrlList, String.class))
                        //数据持久化
                        .addPipeline(new RegionPipeline(sysRegionService, redisTemplate))
                        //开启x个线程抓取
                        .thread(1)
                        //启动爬虫
                        .run();
                return;
            }
            case CITY: {
                Spider.create(new CityProcessor(sysRegionService, redisTemplate, recursionFlag))
                        //从"https://github.com/code4craft"开始抓
                        .addUrl(ArrayUtil.toArray(pageUrlList, String.class))
                        //数据持久化
                        .addPipeline(new RegionPipeline(sysRegionService, redisTemplate))
                        //开启x个线程抓取
                        .thread(1)
                        //启动爬虫
                        .run();
                return;
            }
            case COUNTY: {
                Spider.create(new CountyProcessor(sysRegionService, redisTemplate, recursionFlag))
                        //从"https://github.com/code4craft"开始抓
                        .addUrl(ArrayUtil.toArray(pageUrlList, String.class))
                        //数据持久化
                        .addPipeline(new RegionPipeline(sysRegionService, redisTemplate))
                        //开启x个线程抓取
                        .thread(1)
                        //启动爬虫
                        .run();
                return;
            }
            case TOWN: {
                Spider.create(new TownProcessor(sysRegionService, redisTemplate, recursionFlag))
                        //从"https://github.com/code4craft"开始抓
                        .addUrl(ArrayUtil.toArray(pageUrlList, String.class))
                        //数据持久化
                        .addPipeline(new RegionPipeline(sysRegionService, redisTemplate))
                        //开启x个线程抓取
                        .thread(1)
                        //启动爬虫
                        .run();
                return;
            }
            case VILLAGE: {
                Spider.create(new VillageProcessor(sysRegionService, redisTemplate))
                        //从"https://github.com/code4craft"开始抓
                        .addUrl(ArrayUtil.toArray(pageUrlList, String.class))
                        //数据持久化
                        .addPipeline(new RegionPipeline(sysRegionService, redisTemplate))
                        //开启x个线程抓取
                        .thread(1)
                        //启动爬虫
                        .run();
                return;
            }
            default:
                return;
        }
    }

    public static SysRegion setRegionAlias(SysRegion sysRegion) {
        List<String> removeStrList = Arrays.asList("省", "市", "办事处", "居委会", "行政村", "村委会", "村民委员会", "居民委员会");
        removeStrList.forEach(removeStr -> {
            if (StrUtil.isBlank(sysRegion.getSimpleName())) {
                if (StrUtil.endWith(sysRegion.getName(), removeStr)) {
                    sysRegion.setSimpleName(StrUtil.removeSuffix(sysRegion.getName(), removeStr));
                }
            }
            if (StrUtil.isBlank(sysRegion.getAlias())) {
                if (StrUtil.endWith(sysRegion.getName(), removeStr)) {
                    sysRegion.setAlias(StrUtil.removeSuffix(sysRegion.getName(), removeStr));
                }
            }
        });
        if (StrUtil.isBlank(sysRegion.getSimpleName())) {
            sysRegion.setSimpleName(sysRegion.getName());
        }
        if (StrUtil.isBlank(sysRegion.getAlias())) {
            sysRegion.setAlias(sysRegion.getName());
        }
        return sysRegion;
    }
}
